/*
 * Copyright (C) 2016  Nicola Spanti (RyDroid) <dev@nicola-spanti.info>
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 * 
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */


#include <check.h>
#include "PlanetWars2dRT-core/specific/model/game_default.h"
#include "PlanetWars2dRT-core/specific/model/game_observers.h"
#include "check_observable_generic.h"


struct game_t a_game;

void
tests_setup_init(void)
{
  game_init_first_time_only_default(&a_game);
}

void
tests_setup_free(void)
{
  game_destructor(&a_game);
}


bool
game_add_observer_generic(void* a_game, const observer_t* an_observer)
{
  return game_add_observer(a_game, an_observer);
}

void
game_remove_observers_generic(void* a_game)
{
  game_remove_observers(a_game);
}

void
game_notify_observers_generic(void* a_game, void* data)
{
  game_notify_observers(a_game, data);
}

size_t
game_get_nb_observers_generic(void* a_game)
{
  return game_get_nb_observers(a_game);
}

START_TEST(test_add_observer)
{
  test_add_observer_generic(&a_game,
			    game_add_observer_generic);
}
END_TEST

START_TEST(test_remove_observers)
{
  test_remove_observers_generic(&a_game,
				game_add_observer_generic,
				game_remove_observers_generic,
				game_get_nb_observers_generic);
}
END_TEST

START_TEST(test_notify_observers)
{
  test_notify_observers_generic(&a_game,
				game_add_observer_generic,
				game_notify_observers_generic);
}
END_TEST


Suite*
game_observers_suite(void)
{
  Suite* s = suite_create("Observers of a game");
  
  TCase* tc_generic_observers = tcase_create("Generic observers");
  tcase_add_checked_fixture(tc_generic_observers,
			    tests_setup_init, tests_setup_free);
  tcase_add_test(tc_generic_observers,
		 test_add_observer);
  tcase_add_test(tc_generic_observers,
		 test_remove_observers);
  tcase_add_test(tc_generic_observers,
		 test_notify_observers);
  suite_add_tcase(s, tc_generic_observers);
  
  return s;
}

int
main(void)
{
  size_t number_failed;
  Suite* s;
  SRunner* sr;
  
  s = game_observers_suite();
  sr = srunner_create(s);
  
  srunner_run_all(sr, CK_NORMAL);
  number_failed = srunner_ntests_failed(sr);
  srunner_free(sr);
  
  return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
}
